package com.fuxi.cloud.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/**
 * oConvertUtils
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */
public class oConvertUtils {
    private static final Logger log = LoggerFactory.getLogger(oConvertUtils.class);

    public oConvertUtils() {
    }

    public static boolean isEmpty(Object object) {
        if (object == null) {
            return true;
        } else if ("".equals(object)) {
            return true;
        } else {
            return "null".equals(object);
        }
    }

    public static boolean isNotEmpty(Object object) {
        return object != null && !object.equals("") && !object.equals("null");
    }

    public static String decode(String strIn, String sourceCode, String targetCode) {
        String var3 = code2code(strIn, sourceCode, targetCode);
        return var3;
    }

    private static String code2code(String strIn, String sourceCode, String targetCode) {
        String var3 = null;
        if (strIn != null && !strIn.trim().equals("")) {
            try {
                byte[] var4 = strIn.getBytes(sourceCode);

                for(int var5 = 0; var5 < var4.length; ++var5) {
                    System.out.print(var4[var5] + "  ");
                }

                var3 = new String(var4, targetCode);
                return var3;
            } catch (Exception var6) {
                var6.printStackTrace();
                return null;
            }
        } else {
            return strIn;
        }
    }

    public static int getInt(String s, int defval) {
        if (s != null && s != "") {
            try {
                return Integer.parseInt(s);
            } catch (NumberFormatException var3) {
                return defval;
            }
        } else {
            return defval;
        }
    }

    public static int getInt(String s) {
        if (s != null && s != "") {
            try {
                return Integer.parseInt(s);
            } catch (NumberFormatException var2) {
                return 0;
            }
        } else {
            return 0;
        }
    }

    public static int getInt(String s, Integer df) {
        if (s != null && s != "") {
            try {
                return Integer.parseInt(s);
            } catch (NumberFormatException var3) {
                return 0;
            }
        } else {
            return df;
        }
    }

    public static int getInt(Object object, int defval) {
        if (isEmpty(object)) {
            return defval;
        } else {
            try {
                return Integer.parseInt(object.toString());
            } catch (NumberFormatException var3) {
                return defval;
            }
        }
    }

    public static Integer getInt(Object object) {
        if (isEmpty(object)) {
            return null;
        } else {
            try {
                return Integer.parseInt(object.toString());
            } catch (NumberFormatException var2) {
                return null;
            }
        }
    }

    public static int getInt(BigDecimal s, int defval) {
        return s == null ? defval : s.intValue();
    }

    public static String getString(String s) {
        return getString(s, "");
    }

    public static String getString(Object object) {
        return isEmpty(object) ? "" : object.toString().trim();
    }

    public static String getString(int i) {
        return String.valueOf(i);
    }

    public static String getString(float i) {
        return String.valueOf(i);
    }

    public static String getString(String s, String defval) {
        return isEmpty(s) ? defval : s.trim();
    }

    public static String getString(Object s, String defval) {
        return isEmpty(s) ? defval : s.toString().trim();
    }

    public static String getIp() {
        String var0 = null;

        try {
            InetAddress var1 = InetAddress.getLocalHost();
            var0 = var1.getHostAddress();
        } catch (UnknownHostException var2) {
            var2.printStackTrace();
        }

        return var0;
    }

    public static String camelToUnderline(String para) {
        if (para.length() < 3) {
            return para.toLowerCase();
        } else {
            StringBuilder var1 = new StringBuilder(para);
            int var2 = 0;

            for(int var3 = 2; var3 < para.length(); ++var3) {
                if (Character.isUpperCase(para.charAt(var3))) {
                    var1.insert(var3 + var2, "_");
                    ++var2;
                }
            }

            return var1.toString().toLowerCase();
        }
    }

    public static Field[] getAllFields(Object object) {
        Class var1 = object.getClass();

        ArrayList var2;
        for(var2 = new ArrayList(); var1 != null; var1 = var1.getSuperclass()) {
            var2.addAll(new ArrayList(Arrays.asList(var1.getDeclaredFields())));
        }

        Field[] var3 = new Field[var2.size()];
        var2.toArray(var3);
        return var3;
    }

    public static boolean listIsEmpty(Collection list) {
        return list == null || list.size() == 0;
    }
}
